<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{padding: 0;margin: 0;}
        .phone{
            width: 320px;
            height: 600px;
            border: 1px solid #333;
            margin: 50px auto;
            position: relative;
        }
        .slideBar{
            width: 100%;
            height: 45px;
            background-color: grey;
            position: absolute;
            top: 60%;
        }
        .ball{
            width: 45px;
            height: 45px;
            border-radius: 50%;
            background-color: pink;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>
<body>
    <div class="phone">
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>
    var phone = document.getElementsByClassName("phone")[0];
    var slideBar = document.getElementsByClassName("slideBar")[0];
    var ball = document.getElementsByClassName("ball")[0];

    var maxLeft = slideBar.clientWidth - ball.clientWidth;

    ball.onmousedown = function(e){
        var e = e || window.event;
        var posX = e.offsetX;
        var posY = e.offsetY;

        document.onmousemove = function(e){
            var e = e || window.event;
            var x = e.pageX - phone.offsetLeft - posX;
            // if(x<=0) x = 0;
            // if(x>=maxLeft) x = maxLeft;
            x = Math.min(Math.max(0,x),maxLeft);

            ball.style.left = x + "px";
            if(x == maxLeft){
                location.href = "https://www.baidu.com";
                document.onmousemove = null;
            }

        }

        document.onmouseup = function(e){
            document.onmousemove = null;
            if(ball.offsetLeft != maxLeft){
                ball.style.left = 0 + "px";
            }
        }

    }

   /*  ball.addEventListener("mousedown",mousedownHandler);

    function mousedownHandler(e){
        var e = e || window.event;
        var posX = e.offsetX;
        var posY = e.offsetY;

        document.addEventListener("mousemove",mousemoveHandler);
        // document 本质也是一个对象 (对象新增属性 自定义属性)
        document.posX = posX;
        document.posY = posY;

        document.addEventListener("mouseup",mouseupHandler);


        // var obj = {};
        // obj.a = 1;
    }

    function mousemoveHandler(e){
        // this -> 绑定事件元素(document)

        var e = e || window.event;
        var x = e.pageX - phone.offsetLeft - this.posX;
        // if(x<=0) x = 0;
        // if(x>=maxLeft) x = maxLeft;
        x = Math.min(Math.max(0,x),maxLeft);

        ball.style.left = x + "px";
    }

    function mouseupHandler(e){
        document.removeEventListener("mousemove",mousemoveHandler);
        document.removeEventListener("mouseup",mouseupHandler);
    }

 */


</script>
</html>